﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.Collections.Generic;

namespace BlackBlade.Utilities.SharePoint.OWL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void txtSource_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnConvert_Click(object sender, EventArgs e)
        {

            string SharePointNamespace = "http://schemas.microsoft.com/sharepoint/";
            string strBaseContentTypeID = "0x0100B757FB4D-788B-4d64-A246-79623579C073"; //the ID of the base custom Item content type

            XmlDocument sourceDoc = new XmlDocument();
            sourceDoc.Load(this.txtSourceFile.Text);

            XmlNamespaceManager ns = new XmlNamespaceManager(sourceDoc.NameTable);
            ns.AddNamespace("owl", "http://www.w3.org/2002/07/owl#");
            ns.AddNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
            ns.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
            ns.AddNamespace("sumo", "http://reliant.teknowledge.com/DAML/SUMO.owl#");

            //TODO: add any additional namespace declarations

            Dictionary<string, string> fullNodeIDs = new Dictionary<string, string>();
            Dictionary<string, int> parentChildCounts = new Dictionary<string, int>();

            XmlDocument targetDoc = new XmlDocument();
            XmlElement xmeElements = (XmlElement)targetDoc.AppendChild(targetDoc.CreateElement("Elements", SharePointNamespace));

            XmlNodeList xmeClasses = sourceDoc.DocumentElement.SelectNodes("owl:Class", ns);
            foreach (XmlElement xmeClass in xmeClasses)
            {
                XmlElement xmeContentType = (XmlElement)xmeElements.AppendChild(xmeElements.OwnerDocument.CreateElement("ContentType", SharePointNamespace));

                string strParentID = (string)(xmeClass["rdfs:subClassOf"] != null
                                        ? fullNodeIDs[xmeClass["rdfs:subClassOf"].Attributes["rdf:resource"].Value.Replace("#", "")]
                                        : strBaseContentTypeID);

                //incrament the count of the parent's direct child content types
                int childCount = 1;
                try
                {
                    childCount += parentChildCounts[strParentID];
                }
                catch
                { }
                parentChildCounts[strParentID] = childCount;

                //string strFullID = strParentID + "00" + string.Format("{0:X}", childCount);
                string strFullID = strParentID + (childCount < 16 ? "0" : "") + string.Format("{0,00:X}", childCount);

                //generate the content type id
                // if the class inherits from another class, then get the info from the lookup table
                // otherwise, inherit from the base content type ID of the base custom item content type 
                fullNodeIDs[xmeClass.Attributes["rdf:ID"].Value] = strFullID;

                xmeContentType.Attributes.Append(xmeElements.OwnerDocument.CreateAttribute("ID")).Value =
                    fullNodeIDs[xmeClass.Attributes["rdf:ID"].Value];

                xmeContentType.Attributes.Append(xmeElements.OwnerDocument.CreateAttribute("Name")).Value =
                    xmeClass.Attributes["rdfs:label"].Value;

                xmeContentType.Attributes.Append(xmeElements.OwnerDocument.CreateAttribute("Group")).Value =
                    "Custom Core taxonomy";

                xmeContentType.Attributes.Append(xmeElements.OwnerDocument.CreateAttribute("Description")).Value =
                    xmeClass["rdfs:comment"].InnerText;

                xmeContentType.Attributes.Append(xmeElements.OwnerDocument.CreateAttribute("V2ListTemplateName")).Value =
                    "custlist";

                xmeContentType.Attributes.Append(xmeElements.OwnerDocument.CreateAttribute("Version")).Value =
                    "0";
            }

            this.txtResult.Text = targetDoc.OuterXml;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            switch (this.openFileDialog1.ShowDialog())
            {
                case DialogResult.OK:
                    this.txtSourceFile.Text = this.openFileDialog1.FileName;
                    break;

                default:
                    return;
            }


        }
    }
}
